<?php
function checkSession() {
	if($_SESSION['user_id_sess']=="" || $_SESSION['user_name']=="" || $_SESSION['user_type_sess']=="") 
	{	
		return false;
	}
	else {
		$user['id']=$_SESSION['user_id_sess'];
		$user['name']=$_SESSION['user_name'];
		$user['type']=$_SESSION['user_type_sess'];
		return $user;
	}
}

function signout() {
	session_unset();
	setcookie("swapen_id", $userId, time()-3600*24);
	setcookie("swapen_name", $_POST['username'], time()-3600*24);
	setcookie("swapen_type", $userType, time()-3600*24);
}

function wordFormater($str,$len)
{
$cnt=strlen($str);
if($cnt>=$len)
{
  $str=substr($str, 0, $len);
  $str=$str."...";
}
return $str;
}

function getRemainingTime($durationDate) {
if($durationDate=="") {
	return false;
} else {
	$dateDiff = strtotime($durationDate) - time();
	$fullDays = floor($dateDiff/(60*60*24));
	$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
	$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
	$timeLeft = $fullDays." day ".$fullHours." hr ".$fullMinutes." min";
	return $timeLeft;
	}
}

//function to get product thumb
function getProductImage($proId) {
	$sql = "SELECT * FROM tbl_product_images WHERE product_id='$proId'";
	$rst = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($rst)>0) {
		$row = mysql_fetch_array($rst);
		if($row['product_thumbnail_1']!="") {
			$data['thumb'] = $row['product_thumbnail_1'];
			$data['image'] = $row['product_sample_1'];
			return $data;
		} else if($row['product_thumbnail_2']!="") {
			$data['thumb'] = $row['product_thumbnail_2'];
			$data['image'] = $row['product_sample_2'];
			return $data;
		} else if($row['product_thumbnail_3']!="") {
			$data['thumb'] = $row['product_thumbnail_3'];
			$data['image'] = $row['product_sample_3'];
			return $data;	
		} else if($row['product_thumbnail_4']!="") {
			$data['thumb'] = $row['product_thumbnail_4'];
			$data['image'] = $row['product_sample_4'];
			return $data;
		} else if($row['product_thumbnail_5']!="") {
			$data['thumb'] = $row['product_thumbnail_5'];
			$data['image'] = $row['product_sample_5'];
			return $data;
		} else {
			return false;
		}
	} else {
		return false;
	}
}


function getProductOwner($productId) {
	$sql="SELECT posted_by from tbl_product where product_id='$productId'";
	$rst=mysql_query($sql);
	$row=mysql_fetch_row($rst);
	return $row;
}

function getProductById($productId) {
	$sql="SELECT * FROM tbl_product WHERE product_id='$productId'";
	$rst=mysql_query($sql);
	if($rst) { $row=mysql_fetch_array($rst); return $row; }
	else { return false; }
	}
//get state name by state id
function getStateName($stateId) {
	$sqlSelState = "SELECT statename FROM tbl_state where stateid='".$stateId."'";
	$rstSelState = mysql_query($sqlSelState);
	if($rstSelState) {
		$rowSelState = mysql_fetch_row($rstSelState);
		return $rowSelState[0];
	} else {
		return false;
	}
}

//get city name by city id
function getCityName($cityId) {
	$sqlSelCity = "SELECT cityname FROM tbl_city where cityid='".$cityId."'";
	$rstSelCity = mysql_query($sqlSelCity);
	if($rstSelCity) {
		$rowSelCity = mysql_fetch_row($rstSelCity);
		return $rowSelCity[0];
	} else {
		return false;
	}
}
function getCityStateOfUser($userId) {
	$sql = "SELECT city,state FROM tbl_users where user_id='$userId'";
	$rst = mysql_query($sql);	
	$row = mysql_fetch_row($rst);
	$data = array();
	if(is_numeric($row[0])) {
		$sqlSelCity = "SELECT cityname FROM tbl_city where cityid='".$row[0]."'";
		$rstSelCity = mysql_query($sqlSelCity);
		$rowSelCity = mysql_fetch_row($rstSelCity);
		$data['city'] = $rowSelCity[0];
	}
	else {
		$data['city'] = $row[0];
	}
	
	if(is_numeric($row[1])) {
		$sqlSelState = "SELECT statename FROM tbl_state where stateid='".$row[1]."'";
		$rstSelState = mysql_query($sqlSelState);
		$rowSelState = mysql_fetch_row($rstSelState);
		$data['state'] = $rowSelState[0];
	}
	else {
		$data['state'] = $row[1];
	}
	
	return $data;		
}

function getUser($userId) {
	$sql="SELECT username from tbl_users where user_id='$userId'";
	$rst=mysql_query($sql);
	$row=mysql_fetch_row($rst);
	return $row[0];
}

function getImagePath($productId) {
	$sql="SELECT * from tbl_product_images where product_id='$productId'";
	$rst=mysql_query($sql);
	$row=mysql_fetch_array($rst);
	return $row;
}

function getParentId($catId) {
	$sql="SELECT parent_id,category_name from tbl_category where category_id='$catId'";
	$rst=mysql_query($sql);
	$row=mysql_fetch_row($rst);
	return $row[0];
}

function getAllSubcategory($parentId) {
	$sql="SELECT category_id from tbl_category where parent_id='$parentId'";
	$rst=mysql_query($sql);
	while($row=mysql_fetch_array($rst)) {
		$subcat[] = $row[0];
	}
	return $subcat;
}
function getcategoryName($categoryId) {
	$sql="SELECT category_name from tbl_category where category_id='$categoryId'";
	$rst=mysql_query($sql);
	while($row=mysql_fetch_array($rst)) {
		$categoryName = $row['category_name'];
	}
	return $categoryName;
}

//get all category info by category id
function getAllCat($catId) {
	$sql = "SELECT * FROM tbl_category WHERE category_id='$catId'";
	$rst = mysql_query($sql) or die(mysql_error());	
	if(mysql_num_rows($rst)) {
		$row = mysql_fetch_array($rst);
		return $row;
	} else {
		return false;
	}	
}

//get category subcategory sub-subcategory id, name by category
function getCategory($catId)
{
	$cat1=getAllCat($catId);
		
	if($cat1['parent_id']==0) {
		$cat['categoryId'] = $cat1['category_id'];
		$cat['categoryName'] = $cat1['category_name'];
		$cat['subcategoryId'] = "";
		$cat['subcategoryName'] = "";
		$cat['subsubcategoryId'] = "";
		$cat['subsubcategoryName'] = "";
	}
	else {
		$cat2 = getAllCat($cat1['parent_id']);
		if($cat2['parent_id']==0) {
			$cat['categoryId'] = $cat2['category_id'];
			$cat['categoryName'] = $cat2['category_name'];
			$cat['subcategoryId'] = $cat1['category_id'];
			$cat['subcategoryName'] = $cat1['category_name'];
			$cat['subsubcategoryId'] = "";
			$cat['subsubcategoryName'] = "";
		}
		else {
			$cat3 = getAllCat($cat2['parent_id']);
			if($cat3['parent_id']==0) {				
				$cat['categoryId'] = $cat3['category_id'];
				$cat['categoryName'] = $cat3['category_name'];
				$cat['subcategoryId'] = $cat2['category_id'];
				$cat['subcategoryName'] = $cat2['category_name'];
				$cat['subsubcategoryId'] = $cat1['category_id'];
				$cat['subsubcategoryName'] = $cat1['category_name'];
			}
		}
	}
	return $cat;
}

// photo resizer
function imageResize($pathPackage, $pathThumb, $pathSample, $extension) {	
	 
	 list($widthSample, $heightSample) = getimagesize($pathPackage);
	 list($widthThumb, $heightThumb) = getimagesize($pathPackage);
	 //width and height of thumb for photo
	 if($widthSample>300)
	   $toWidthSample=300;
	   else
	   $toWidthSample=300;
	   if($heightSample>300)
	   {
			$toHeightSample=ceil(($heightSample/$widthSample)*300);						
	   }
	   else { $toHeightSample=260; }
	   
	   if($toHeightSample>300)
	   {
	   		$toHeightSample=300;
	   		$toWidthSample=ceil(($widthSample/$heightSample)*300);
	   }
	 
	 //width and height of sample	for photo
	   $toWidthThumb=100;
	   $toHeightThumb=ceil(($heightThumb/$widthThumb)*100);
	
	  if($toHeightThumb>100)
	   {
	   $toHeightThumb=100;
	   $toWidthThumb=ceil(($widthThumb/$heightThumb)*100);
	   }  
			   
   
	$imageResizedSample = imagecreatetruecolor($toWidthSample, $toHeightSample);
	$imageResizedThumb = imagecreatetruecolor($toWidthThumb, $toHeightThumb);
	if (preg_match("/jpg|jpeg|pjpeg/",strtolower($extension)))
	{
	$src_img=imagecreatefromjpeg($pathPackage);
	imagecopyresampled($imageResizedSample,$src_img, 0, 0, 0, 0, $toWidthSample, $toHeightSample, $widthSample, $heightSample);	
	imagecopyresampled($imageResizedThumb,$src_img, 0, 0, 0, 0, $toWidthThumb, $toHeightThumb, $widthThumb, $heightThumb);	
	imagejpeg($imageResizedThumb,$pathThumb,100);
	imagejpeg($imageResizedSample,$pathSample,100);					
	}
	if (preg_match("/png/",strtolower($extension)))
	{
	$src_img=imagecreatefrompng($pathPackage);					
	imagecopyresampled($imageResizedSample,$src_img, 0, 0, 0, 0, $toWidthSample, $toHeightSample, $widthSample, $heightSample);
	imagecopyresampled($imageResizedThumb,$src_img, 0, 0, 0, 0, $toWidthThumb, $toHeightThumb, $widthThumb, $heightThumb);
	imagepng($imageResizedSample,$pathSample,9);
	imagepng($imageResizedThumb,$pathThumb,9);
	}
	
	if (preg_match("/gif/",strtolower($extension)))
	{
	$src_img=imagecreatefromgif($pathPackage);
	imagecopyresampled($imageResizedSample,$src_img, 0, 0, 0, 0, $toWidthSample, $toHeightSample, $widthSample, $heightSample);
	imagecopyresampled($imageResizedThumb,$src_img, 0, 0, 0, 0, $toWidthThumb, $toHeightThumb, $widthThumb, $heightThumb);
	imagegif($imageResizedSample,$pathSample,100);
	imagegif($imageResizedThumb,$pathThumb,100);
	}
	$msg="true";
	return $msg;
}



function priceConvert($catPrice)
 {
	$str=explode(".",$catPrice);
	$first=$str[0];
	$second=$str[1];
	$length= strlen($second);
	if(empty($second))
	 {
		$catPriceIns=$first.".00";
	 }
	else if($length==2)
	 {
		$catPriceeIns=$catPrice;
	 }
	else if($length==1)
	 {
		$catPriceIns=$catPrice."0";
	 }
	else if($length>2)
	 {
		$catPriceIns=round($catPrice,2);
	 }
	return $catPriceIns;
 }
 function assign_rand_value($num)
{
  switch($num)
  {
    case "1":
     $rand_value = "a";
    break;
    case "2":
     $rand_value = "b";
    break;
    case "3":
     $rand_value = "c";
    break;
    case "4":
     $rand_value = "d";
    break;
    case "5":
     $rand_value = "e";
    break;
    case "6":
     $rand_value = "f";
    break;
    case "7":
     $rand_value = "g";
    break;
    case "8":
     $rand_value = "h";
    break;
    case "9":
     $rand_value = "i";
    break;
    case "10":
     $rand_value = "j";
    break;
    case "11":
     $rand_value = "k";
    break;
    
    case "13":
     $rand_value = "m";
    break;
    case "14":
     $rand_value = "n";
    break;
    case "16":
     $rand_value = "p";
    break;
    case "17":
     $rand_value = "q";
    break;
    case "18":
     $rand_value = "r";
    break;
    case "19":
     $rand_value = "s";
    break;
    case "20":
     $rand_value = "t";
    break;
    case "21":
     $rand_value = "u";
    break;
    case "22":
     $rand_value = "v";
    break;
    case "23":
     $rand_value = "w";
    break;
    case "24":
     $rand_value = "x";
    break;
    case "25":
     $rand_value = "y";
    break;
    case "26":
     $rand_value = "z";
    break;
    case "27":
     $rand_value = "Y";
    break;
    case "28":
     $rand_value = "Z";
    break;
    case "29":
     $rand_value = "2";
    break;
    case "30":
     $rand_value = "3";
    break;
    case "31":
     $rand_value = "4";
    break;
    case "32":
     $rand_value = "5";
    break;
    case "33":
     $rand_value = "6";
    break;
    case "34":
     $rand_value = "7";
    break;
    case "35":
     $rand_value = "8";
    break;
    case "36":
     $rand_value = "9";
    break;
    case "37":
     $rand_value = "A";
    break;
    case "38":
     $rand_value = "B";
    break;
    case "39":
     $rand_value = "C";
    break;
    case "40":
     $rand_value = "D";
    break;
    case "41":
     $rand_value = "E";
    break;
    case "42":
     $rand_value = "F";
    break;
    case "43":
     $rand_value = "G";
    break;
    case "44":
     $rand_value = "H";
    break;
    case "45":
     $rand_value = "I";
    break;
    case "46":
     $rand_value = "J";
    break;
    case "47":
     $rand_value = "K";
    break;
    
    case "48":
     $rand_value = "M";
    break;
    case "49":
     $rand_value = "N";
    break;
    case "50":
     $rand_value = "P";
    break;
    case "51":
     $rand_value = "Q";
    break;
    case "52":
     $rand_value = "R";
    break;
    case "53":
     $rand_value = "S";
    break;
    case "54":
     $rand_value = "T";
    break;
    case "55":
     $rand_value = "U";
    break;
    case "56":
     $rand_value = "V";
    break;
    case "57":
     $rand_value = "W";
    break;
    case "58":
     $rand_value = "X";
    break;
     case "59":
     $rand_value = "2";
    break;
    case "60":
     $rand_value = "3";
    break;
    case "61":
     $rand_value = "4";
    break;
    case "62":
     $rand_value = "5";
    break;
    case "63":
     $rand_value = "6";
    break;
    case "64":
     $rand_value = "7";
    break;
    case "65":
     $rand_value = "8";
    break;
    case "66":
     $rand_value = "9";
    break;
  }
return $rand_value;
}
function get_rand_id($length)
{
  if($length>0) 
  { 
  $rand_id="";
   for($i=1; $i<=$length; $i++)
   {
   mt_srand((double)microtime() * 1000000);
   $num = mt_rand(1,66);
   $rand_id .= assign_rand_value($num);
   }
  }
return $rand_id;
}


function getWishList()
{
if($_SESSION['user_id_sess']!="")
{
$sqlSelLst="SELECT * FROM tbl_wishlist where user_id=".$_SESSION['user_id_sess']." ORDER BY product_order LIMIT 0,3";
$rstSelLst=mysql_query($sqlSelLst);			
$numSelLst=mysql_num_rows($rstSelLst);	  
if($numSelLst>0)
{
$i=1;
echo "<td height='123' style='padding-left:5px; padding-right:5px; line-height:20px' valign='top'>";
while($rowSelLst=mysql_fetch_array($rstSelLst))
{
$wishId=$rowSelLst['wishlist_id'];
$wishProductName=$rowSelLst['product_name'];
echo $i.". ".wordFormater($wishProductName,18)."<br>";
$i++;
}
if($i>3)
 echo "<a href='index.php?wlist'>View more</a>";
 echo "</td>";
}
else
  echo "Create your wishlist";
}
else
 echo "<td height='123' style='padding-left:5px; padding-right:5px; line-height:20px'>
        <center>What is <br>your wish ?</center>
		</td>";
}

function checkShippingDetails($sellingType)
{
if($sellingType!='')
 {
	if($sellingType=='both')
	 {
		$shippingAvailability="Available";
		$pickupAvailability="Available";
	 }
	else if($sellingType=='online')
	 {
		$shippingAvailability="Available";
		$pickupAvailability="Not available";
	 }
    else if($sellingType=='local')
	 {
		$shippingAvailability="Not available";
		$pickupAvailability="Available";
	 }
	 $shippingDetails="<strong>Shipping</strong> : ".$shippingAvailability."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>Local pickup</strong> : ".$pickupAvailability;
	 return $shippingDetails;
  }
}
function getImageCount($prodId)
{
$sqlSelImg="SELECT * FROM tbl_product_images WHERE product_id=".$prodId;
$rstSelImg=mysql_query($sqlSelImg);
$rowSelImg=mysql_fetch_array($rstSelImg);
$count=0;
for($i=1;$i<6;$i++)
{
if(!empty($rowSelImg['product_thumbnail_'.$i]))
$count=$count+1;
}
return $count;
}
function getUserType($userId)
{
$sqlSelUserType="SELECT user_type FROM tbl_users WHERE user_id='$userId'";
$rstSelUserType=mysql_query($sqlSelUserType);
$rowSelUserType=mysql_fetch_array($rstSelUserType);	
$userType=$rowSelUserType['user_type'];
return $userType;
}
?>